package com.cssy.xxkj.basic.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.byd.acegiext.domain.User;
import com.cssy.xxkj.basic.dao.BasicMaterialsDAO;
import com.cssy.xxkj.basic.dao.BasicMaterialsTypeDAO;
import com.cssy.xxkj.basic.dao.BasicProcessDAO;
import com.cssy.xxkj.basic.dao.BasicProductBomDAO;
import com.cssy.xxkj.basic.dao.BasicProductBomMaterialsDAO;
import com.cssy.xxkj.basic.dao.BasicProductBomProcessDAO;
import com.cssy.xxkj.basic.dao.BasicStapleUnitDAO;
import com.cssy.xxkj.basic.dao.BasicWorkshopDAO;
import com.cssy.xxkj.basic.entity.BasicMaterialsDO;
import com.cssy.xxkj.basic.entity.BasicMaterialsTypeDO;
import com.cssy.xxkj.basic.entity.BasicProcessDO;
import com.cssy.xxkj.basic.entity.BasicProductBomDO;
import com.cssy.xxkj.basic.entity.BasicProductBomMaterialsDO;
import com.cssy.xxkj.basic.entity.BasicProductBomProcessDO;
import com.cssy.xxkj.basic.entity.BasicStapleUnitDO;
import com.cssy.xxkj.basic.entity.BasicWorkshopDO;
import com.cssy.xxkj.basic.service.IBasicMaterialsBomService;
import com.plat.exception.SyBaseException;
import com.plat.helper.SearchConditions;
import com.plat.helper.SearchResult;
import com.plat.util.GlobalResLoader;
import com.plat.util.IDCodeTypeUtil;
import com.plat.util.SessionUtil;
import com.yuendong.sy.po.code.SyCode;
import com.yuendong.sy.service.code.impl.SyCodeService;


@Service("basicMaterialsBomService")
public class BasicMaterialsBomServiceImpl implements IBasicMaterialsBomService{

	private final Map<String,BasicProcessDO> processMap= new HashMap<String, BasicProcessDO>();
	private final Map<String,BasicMaterialsDO> materialsMap= new HashMap<String, BasicMaterialsDO>();
	private final Map<String,BasicMaterialsTypeDO> materialsTypeMap= new HashMap<String, BasicMaterialsTypeDO>();
	private final Map<String,BasicStapleUnitDO> stapleUnitMap= new HashMap<String, BasicStapleUnitDO>();
	private final Map<String,BasicWorkshopDO> wordshopMap = new HashMap<String, BasicWorkshopDO>();
	 //  
	@Resource(name="basicProductBomDAO")	
	private BasicProductBomDAO basicProductBomDAO;
	//基本单位DAO
	@Resource(name="basicStapleUnitDAO")	
	private BasicStapleUnitDAO basicStapleUnitDAO;
	//物料类型DAO
	@Resource(name="basicMaterialsTypeDAO")	
	private BasicMaterialsTypeDAO basicMaterialsTypeDAO;
	
	//物料DAO
	@Resource(name="basicMaterialsDAO")	
	private BasicMaterialsDAO basicMaterialsDAO;
	
	//工序DAO
	@Resource(name="basicProcessDAO")	
	private BasicProcessDAO basicProcessDAO;
	
	//车间DAO
	@Resource(name="basicWorkshopDAO")	
	private BasicWorkshopDAO basicWorkshopDAO;
	
	@Resource(name="basicProductBomMaterialsDAO")	
	private BasicProductBomMaterialsDAO basicProductBomMaterialsDAO;
	
	@Resource(name="basicProductBomProcessDAO")	
	private BasicProductBomProcessDAO basicProductBomProcessDAO;
	
	@Resource(name="syCodeService")
	private SyCodeService syCodeService;
	
	@Override
	public SearchResult findListByExample(SearchConditions scs)
			throws SyBaseException {
		SearchResult rs=new SearchResult(scs);
		List<BasicProductBomDO>  list=basicProductBomDAO.find(scs);
		if(list!=null&&!list.isEmpty()&&list.size()>0){
			if(materialsMap==null||materialsMap.isEmpty()||materialsMap.size()==0){
				materialsMap.putAll(this.materialsMap());
			}
			if(materialsTypeMap==null||materialsTypeMap.isEmpty()||materialsTypeMap.size()==0){
				materialsTypeMap.putAll(this.materialsTypeMap());
			}
			for(int i=0;i<list.size();i++){
				list.get(i).setBasicMaterialsDO(materialsMap.get(list.get(i).getZd008MaterialsId()));
				list.get(i).setBasicMaterialsTypeDO(materialsTypeMap.get(list.get(i).getZd008MaterTypeId()));
			}
		}
		rs.setRecordCount(basicProductBomDAO.count(scs));
		rs.setResults(list);
		return rs;
	}

	@Override
	public Integer delete(String id, User user) throws SyBaseException {
		return basicProductBomDAO.deleteBasicProductBomDOByPrimaryKey(id);
	}

	@Override
	public Integer update(BasicProductBomDO t, User user) throws SyBaseException {
		t.setBizModifyName(user.getName());
		t.setBizModifyDate(new Date());
		t.setBizVersionMark(t.getBizVersion());
		t.setBizVersion(t.getBizVersion()+1);
		return basicProductBomDAO.updateBasicProductBomDO(t);
	}

	@Override
	public BasicProductBomDO save(BasicProductBomDO t, User user)
			throws SyBaseException {
		t.setBizCreatorDate(new Date());
		t.setBizCreatorName(user.getName());
		t.setBizVersion(1);
		basicProductBomDAO.insertBasicProductBomDO(t);
		return t;
	}

	@Override
	public BasicProductBomDO findListById(String id) throws SyBaseException {
		BasicProductBomDO basicProductBomDO=basicProductBomDAO.findBasicProductBomDOByPrimaryKey(id);
		if(basicProductBomDO!=null&&basicProductBomDO.getId()!=null){
			if(materialsMap==null||materialsMap.isEmpty()||materialsMap.size()==0){
				materialsMap.putAll(this.materialsMap());
			}
			if(materialsTypeMap==null||materialsTypeMap.isEmpty()||materialsTypeMap.size()==0){
				materialsTypeMap.putAll(this.materialsTypeMap());
			}
			basicProductBomDO.setBasicMaterialsDO(materialsMap.get(basicProductBomDO.getZd008MaterialsId()));
			basicProductBomDO.setBasicMaterialsTypeDO(materialsTypeMap.get(basicProductBomDO.getZd008MaterTypeId()));
		}
		return basicProductBomDO;
	}

	@Override
	public Integer deleteBizRemoved(String id) throws SyBaseException {
		return basicProductBomDAO.deleteBizRemoved(id);
	}
	
	@Override
	public void bomSave(HttpServletRequest request, BasicProductBomDO curAP)
			throws SyBaseException {
		try{
			String materItem =request.getParameter("materItem");
			String materItemDel =request.getParameter("materItemDel");
			String processItem =request.getParameter("processItem");
			String processItemDel =request.getParameter("processItemDel");
			//新增
			if(curAP.getId()==null||"".equals(curAP.getId())){
				String id = IDCodeTypeUtil.getId();
				curAP.setId(id);
				curAP.setZd001Code(syCodeService.getCode("BOM","BOM",SyCode.DATE_YYMM,5));
				this.save(curAP, SessionUtil.getUser(request));
				if(materItem!=null&&!"".equals(materItem)){
					List<BasicProductBomMaterialsDO> axxlist = JSONArray.parseArray(materItem,BasicProductBomMaterialsDO.class);
					if(axxlist!=null&&!axxlist.isEmpty()&&axxlist.size()>0){
						for(int i=0;i<axxlist.size();i++){
							axxlist.get(i).setId(IDCodeTypeUtil.getId());
							axxlist.get(i).setZd001BomId(id);
							basicProductBomMaterialsDAO.insertBasicProductBomMaterialsDO(axxlist.get(i));
						}
					}
				}
				if(processItem!=null&&!"".equals(processItem)){
					List<BasicProductBomProcessDO> axxlist = JSONArray.parseArray(processItem,BasicProductBomProcessDO.class);
					if(axxlist!=null&&!axxlist.isEmpty()&&axxlist.size()>0){
						for(int i=0;i<axxlist.size();i++){
							axxlist.get(i).setId(IDCodeTypeUtil.getId());
							axxlist.get(i).setZd001BomId(id);
							basicProductBomProcessDAO.insertBasicProductBomProcessDO(axxlist.get(i));
						}
					}
				}
			}else{
				int rt=0;
				rt=this.update(curAP, SessionUtil.getUser(request));
				if(0==rt){
					throw new SyBaseException("您修改的行数据发生改变！请重新打开查看");
				}
				if(materItem!=null&&!"".equals(materItem)){
					List<BasicProductBomMaterialsDO> axxlist = JSONArray.parseArray(materItem,BasicProductBomMaterialsDO.class);
					if(axxlist!=null&&!axxlist.isEmpty()&&axxlist.size()>0){
						basicProductBomMaterialsDAO.deleteBomId(curAP.getId());
						for(int i=0;i<axxlist.size();i++){
							axxlist.get(i).setId(IDCodeTypeUtil.getId());
							axxlist.get(i).setZd001BomId(curAP.getId());
							basicProductBomMaterialsDAO.insertBasicProductBomMaterialsDO(axxlist.get(i));
						}
					}
				}
				if(materItemDel!=null&&!"".equals(materItemDel)){
					List<BasicProductBomMaterialsDO> materList = JSONArray.parseArray(materItemDel,BasicProductBomMaterialsDO.class);
					if(materList!=null&&!materList.isEmpty()&&materList.size()>0){
						for(int i=0;i<materList.size();i++){
							BasicProductBomMaterialsDO bomMater= materList.get(i);
							if(bomMater==null||StringUtils.isBlank(bomMater.getId())){
								continue;
							}
							basicProductBomMaterialsDAO.deleteBasicProductBomMaterialsDOByPrimaryKey(bomMater.getId());
						}
					}
				}
				if(processItem!=null&&!"".equals(processItem)){
					List<BasicProductBomProcessDO> axxlist = JSONArray.parseArray(processItem,BasicProductBomProcessDO.class);
					if(axxlist!=null&&!axxlist.isEmpty()&&axxlist.size()>0){
						basicProductBomProcessDAO.deleteBomId(curAP.getId());
						for(int i=0;i<axxlist.size();i++){
							axxlist.get(i).setId(IDCodeTypeUtil.getId());
							axxlist.get(i).setZd001BomId(curAP.getId());
							basicProductBomProcessDAO.insertBasicProductBomProcessDO(axxlist.get(i));
						}
					}
				}
				if(processItemDel!=null&&!"".equals(processItemDel)){
					List<BasicProductBomProcessDO> proceList = JSONArray.parseArray(processItemDel,BasicProductBomProcessDO.class);
					if(proceList!=null&&!proceList.isEmpty()&&proceList.size()>0){
						for(int i=0;i<proceList.size();i++){
							BasicProductBomProcessDO processBom = proceList.get(i);
							if(processBom==null||StringUtils.isBlank(processBom.getId())){
								continue;
							}
							basicProductBomProcessDAO.deleteBasicProductBomProcessDOByPrimaryKey(processBom.getId());
						}
					}
				}
			}
		}catch(Exception e){
			e.printStackTrace();
			throw new SyBaseException(GlobalResLoader.getOpfailed()
					+ e.getMessage());
		}
		
	}

	@Override 
	public SearchResult materList(BasicProductBomMaterialsDO basicProductBomMaterialsDO) throws SyBaseException {
		SearchResult rs=new SearchResult();
		List<BasicProductBomMaterialsDO> list =basicProductBomMaterialsDAO.findListByExample(basicProductBomMaterialsDO);
		if(list!=null&&!list.isEmpty()&&list.size()>0){
			if(materialsMap==null||materialsMap.isEmpty()||materialsMap.size()==0){
				materialsMap.putAll(this.materialsMap());
			}
			if(materialsTypeMap==null||materialsTypeMap.isEmpty()||materialsTypeMap.size()==0){
				materialsTypeMap.putAll(this.materialsTypeMap());
			}
			if(processMap==null||processMap.isEmpty()||processMap.size()==0){
				processMap.putAll(this.processMap());
			}
			for(int i=0;i<list.size();i++){
				list.get(i).setBasicMaterialsDO(materialsMap.get(list.get(i).getZd003MaterialsId()));
				list.get(i).setBasicMaterialsTypeDO(materialsTypeMap.get(list.get(i).getZd004MaterTypeId()));
				list.get(i).setBasicProcessDO(processMap.get(list.get(i).getZd002ProcessId()));
			}
		}
		rs.setRecordCount(list.size());
		rs.setResults(list);
		return rs;
	}
	
	@Override
	public Map<String, String> findProductionBomToMap(Map<String, Object> paramMap)throws SyBaseException{
		SearchConditions prodBomScs = new SearchConditions();
		List<BasicProductBomDO> prodBomList=basicProductBomDAO.find(prodBomScs);
		if(prodBomList==null||prodBomList.isEmpty()||prodBomList.size()==0){
			return null;
		}else{
			Map<String, String> prodBomMap = new HashMap<String, String>();
			for(BasicProductBomDO prodBom : prodBomList){
				if(prodBom==null||StringUtils.isBlank(prodBom.getId())){
					continue;
				}
				String keyStr = prodBom.getZd008MaterialsId();
				prodBomMap.put(keyStr, prodBom.getId());
			}
			return prodBomMap;
		}
	}
	
	/**
	 * 查询产品bom的物料id组合成map
	 */
	@Override
	public Map<String, List<String>> findProductionBomMaterToMap(Map<String, Object> paramMap)throws SyBaseException{
		BasicProductBomMaterialsDO prodBomMater = new BasicProductBomMaterialsDO();
		List<BasicProductBomMaterialsDO> bomMaterList = basicProductBomMaterialsDAO.findListByExample(prodBomMater);
		if(bomMaterList==null||bomMaterList.isEmpty()||bomMaterList.size()==0){
			return null;
		}else{
			Map<String, List<String>> bomMaterIdToMap = new HashMap<String, List<String>>();
			for(BasicProductBomMaterialsDO bomMater : bomMaterList){
				if(bomMater==null||StringUtils.isBlank(bomMater.getId())){
					continue;
				}
				String keyStr = bomMater.getZd001BomId();
				List<String> materIdList = bomMaterIdToMap.get(keyStr);
				if(materIdList==null){
					materIdList = new ArrayList<String>();
					bomMaterIdToMap.put(keyStr, materIdList);
				}
				materIdList.add(bomMater.getZd003MaterialsId());
			}
			return bomMaterIdToMap;
		}
	}

	@Override
	public SearchResult processList(BasicProductBomProcessDO basicProductBomProcessDO) throws SyBaseException {
		SearchResult rs=new SearchResult();
		List<BasicProductBomProcessDO> list=basicProductBomProcessDAO.findListByExample(basicProductBomProcessDO);
		if(list!=null&&!list.isEmpty()&&list.size()>0){
			if(processMap==null||processMap.isEmpty()||processMap.size()==0){
				processMap.putAll(this.processMap());
			}
			for(int i=0;i<list.size();i++){
				list.get(i).setBasicProcessDO(processMap.get(list.get(i).getZd002ProcessId()));
				if(wordshopMap==null||wordshopMap.isEmpty()||wordshopMap.size()==0){
					wordshopMap.putAll(this.workshopMap());
				}
				if(list.get(i).getBasicProcessDO()!=null&&list.get(i).getBasicProcessDO().getId()!=null){
					list.get(i).getBasicProcessDO().setBasicWorkshopDO(wordshopMap.get(list.get(i).getBasicProcessDO().getZd003WorkShop()));
				}
			}
		}
		rs.setRecordCount(list.size());
		rs.setResults(list);
		return rs;
	}
	
	/**
	 * 返回基本单位MAP
	 * @return
	 */
	private Map<String,BasicStapleUnitDO> stapleUnitMap(){
		Map<String,BasicStapleUnitDO> map = new HashMap<String, BasicStapleUnitDO>();
		SearchConditions scs = new SearchConditions();
		scs.setRemovedProperty("removed");
		List<BasicStapleUnitDO>  list=basicStapleUnitDAO.find(scs);
		if(list!=null&&!list.isEmpty()&&list.size()>0){
			for(int i=0;i<list.size();i++){
				map.put(list.get(i).getId(), list.get(i));
			}
		}else{
			return null;
		}
		return map;
	}
	
	/**
	 * 返回物料类型MAP
	 * @return
	 */
	private Map<String,BasicMaterialsTypeDO> materialsTypeMap(){
		Map<String,BasicMaterialsTypeDO> map = new HashMap<String, BasicMaterialsTypeDO>();
		SearchConditions scs = new SearchConditions();
		scs.setRemovedProperty("removed");
		List<BasicMaterialsTypeDO>  list=basicMaterialsTypeDAO.find(scs);
		if(list!=null&&!list.isEmpty()&&list.size()>0){
			for(int i=0;i<list.size();i++){
				map.put(list.get(i).getId(), list.get(i));
			}
		}else{
			return null;
		}
		return map;
	}
	
	/**
	 * 返回物料MAP
	 * @return
	 */
	private Map<String,BasicMaterialsDO> materialsMap(){
		Map<String,BasicMaterialsDO> map = new HashMap<String, BasicMaterialsDO>();
		SearchConditions scs = new SearchConditions();
		scs.setRemovedProperty("removed");
		List<BasicMaterialsDO>  list=basicMaterialsDAO.find(scs);
		if(list!=null&&!list.isEmpty()&&list.size()>0){
			for(int i=0;i<list.size();i++){
				if(stapleUnitMap==null||stapleUnitMap.isEmpty()||stapleUnitMap.size()==0){
					stapleUnitMap.putAll(this.stapleUnitMap());
				}
				if(materialsTypeMap==null||materialsTypeMap.isEmpty()||materialsTypeMap.size()==0){
					materialsTypeMap.putAll(this.materialsTypeMap());
				}
				list.get(i).setBasicStapleUnitDO(stapleUnitMap.get(list.get(i).getZd007Unit()));
				list.get(i).setBasicMaterialsTypeDO(materialsTypeMap.get(list.get(i).getZd029MaterTypeId()));
				map.put(list.get(i).getId(), list.get(i));
			}
		}else{
			return null;
		}
		return map;
	}
	
	/**
	 * 工序MAP
	 * @return
	 */
	private Map<String,BasicProcessDO> processMap(){
		Map<String,BasicProcessDO> map = new HashMap<String, BasicProcessDO>();
		SearchConditions scs = new SearchConditions();
		scs.setRemovedProperty("removed");
		List<BasicProcessDO>  list=basicProcessDAO.find(scs);
		if(list!=null&&!list.isEmpty()&&list.size()>0){
			for(int i=0;i<list.size();i++){
				map.put(list.get(i).getId(), list.get(i));
			}
		}else{
			return null;
		}
		return map;
	}
	
	/**
	 * 车间MAP
	 * @return
	 */
	private Map<String,BasicWorkshopDO> workshopMap(){
		Map<String,BasicWorkshopDO> map = new HashMap<String, BasicWorkshopDO>();
		SearchConditions scs = new SearchConditions();
		scs.setRemovedProperty("removed");
		List<BasicWorkshopDO>  list=basicWorkshopDAO.find(scs);
		if(list!=null&&!list.isEmpty()&&list.size()>0){
			for(int i=0;i<list.size();i++){
				map.put(list.get(i).getId(), list.get(i));
			}
		}else{
			return null;
		}
		return map;
	}
}
